package com.cordys.uc.webapps.translator;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

public class Translator {

    private static final String svnFolder = ".svn";
    private static final String cafExtension = ".caf";
    private static final String htmlExtension = ".htm";
    private static final String javascriptExtension = ".js";
    private static final String WORKING_DIRECTORY = "user.dir";
    private static final String SRC_DIRECTORY = "products" + File.separator + "webapps" + File.separator + "src";
    private static final String WEB_DIRECTORY = SRC_DIRECTORY + File.separator + "web";
    private static final String CAF_DIRECTORY = SRC_DIRECTORY + File.separator + "isvpcontent" + File.separator + "xmlstore"
            + File.separator + "Cordys" + File.separator + "WCP" + File.separator + "XForms" + File.separator + "runtime";
    private static String EXCEL_FILE; 
   

    public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {
        
    	EXCEL_FILE = System.getProperty(WORKING_DIRECTORY)+ File.separator + "Translationlogs" + File.separator + "Labels.xls";
    	TranslationLogger.EXCEPTIONLOGFILE = System.getProperty(WORKING_DIRECTORY)+ File.separator + "Translationlogs" + File.separator + "exception.log";
    	File excel=new File(EXCEL_FILE);
        if(excel!=null && excel.exists())
        {
            excel.delete();
        }
        
        String projectPath = System.getProperty(WORKING_DIRECTORY);
        System.out.println("Current Working Directory" + projectPath);

        File projectWebDir = new File(projectPath + File.separator + WEB_DIRECTORY);
        processWebContent(projectWebDir);

        File projectCAFDir = new File(projectPath + File.separator + CAF_DIRECTORY);
        processCAFContent(projectCAFDir);
        try {
            TranslationLogger.wrlog_exception.close();
        } catch (IOException ex) {
            Logger.getLogger(Translator.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static void processWebContent(File webContentFile) {
        String fileName = webContentFile.getName();
        if (webContentFile.isDirectory() && !svnFolder.equalsIgnoreCase(fileName)) {
            System.out.println("Processing Folder " + fileName);
            File[] fileList = webContentFile.listFiles();
            for (File file : fileList) {
                processWebContent(file);
            }
        } else if (webContentFile.isFile()) {
            if (htmlExtension.equalsIgnoreCase(fileName.substring(fileName.lastIndexOf(".")))) {
                System.out.println("\t\tProcessing html file " + fileName);
                (new HTMLTranslator(webContentFile)).translate();
            } else if (javascriptExtension.equalsIgnoreCase(fileName.substring(fileName.lastIndexOf(".")))) {
                System.out.println("\t\tProcessing javascript file " + fileName);
                (new JavaScriptTranslator(webContentFile, EXCEL_FILE)).translate();
            }
        }
    }

    private static void processCAFContent(File cafContentFile) throws SAXException, IOException, ParserConfigurationException {
        String fileName = cafContentFile.getName();
        if (cafContentFile.isDirectory() && !svnFolder.equalsIgnoreCase(fileName)) {
            System.out.println("Processing Folder " + fileName);
            File[] fileList = cafContentFile.listFiles();
            for (File file : fileList) {
                processCAFContent(file);
            }
        } else if (cafContentFile.isFile()) {
            if (cafExtension.equalsIgnoreCase(fileName.substring(fileName.lastIndexOf(".")))) {
                System.out.println("\t\tProcessing caf file " + fileName);
                (new XFormTranslator(cafContentFile)).translate();
            }
        }
    }
}
